package store

import (
	"fmt"
	"jihe.common/protos/base_server/proto"
	"jihe.common/tool"
	"time"
)

func (s *Model) Create(in *proto.DStore) (err error) {
	sqlStr := fmt.Sprintf("INSERT INTO %s (id, name, description, d_uid)VALUES($1,$2,$3,$4)", s.Name)
	_, err = s.Db.Exec(sqlStr, tool.GetSnowflakeId(), in.Name, in.Description, in.DUid)
	if err != nil {
		return
	}
	return
}

func (s *Tx) Delete(worksCollectionId string) (err error) {
	sqlStr := fmt.Sprintf("DELETE FROM %s WHERE works_collection_id = $1", s.Name)
	_, err = s.Db.Exec(sqlStr, worksCollectionId)
	if err != nil {
		return
	}
	return
}

func (s *Model) GetByDuid(dUid string) (ret *proto.DStore, err error) {
	ret = new(proto.DStore)
	sqlStr := fmt.Sprintf("select id, name, description, d_uid, created_at, updated_at from %s where d_uid = $1", s.Name)
	var (
		createTime *time.Time
		updateTime *time.Time
	)
	err = s.Db.QueryRow(sqlStr, dUid).Scan(&ret.Id, &ret.Name, &ret.Description, &ret.DUid, &createTime, &updateTime)
	if err != nil {
		return
	}
	return
}
